package jp.dev7.hongo2;

import java.util.List;

import jp.dev7.hongo2.entity.Question;
import jp.dev7.hongo2.util.Db4oHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

	private static final int MENU_DOWNLOAD = 0;
	private static final int MENU_LIST = 1;

	private static final int REQUEST_DOWNLOAD = 0;

	private TextView content;

	private Button[] selections = new Button[4];

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		content = (TextView) findViewById(R.id.main_content);
		selections[0] = (Button) findViewById(R.id.main_selection0);
		selections[1] = (Button) findViewById(R.id.main_selection1);
		selections[2] = (Button) findViewById(R.id.main_selection2);
		selections[3] = (Button) findViewById(R.id.main_selection3);

		for (Button selection : selections) {
			selection.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					for (int i = 0; i < selections.length; i++) {
						if (selections[i] == v) {
							onSelect(i);
						}
					}
				}
			});
		}

		Db4oHelper helper = new Db4oHelper(this);
		List<Question> list = helper.findAllQuestion();

		for (Question question : list) {
			Toast.makeText(this, "q=" + question.content, Toast.LENGTH_LONG)
					.show();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		content.setText("問題文");
		selections[0].setText("選択肢1");
		selections[1].setText("選択肢2");
		selections[2].setText("選択肢3");
		selections[3].setText("選択肢4");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_DOWNLOAD, 0, "ダウンロード");
		menu.add(0, MENU_LIST, 0, "一覧");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_DOWNLOAD: {
			Intent i = new Intent(this, Download.class);
			startActivityForResult(i, REQUEST_DOWNLOAD);
			break;
		}
		case MENU_LIST: {
			Intent i = new Intent(this, QuestionList.class);
			startActivity(i);
			break;
		}
		}
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_DOWNLOAD: {
			if (resultCode == RESULT_OK) {
				Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
			}
			break;
		}
		}
	}

	private void onSelect(int i) {
		Toast.makeText(this, "選択 = " + i, Toast.LENGTH_SHORT).show();
	}
}